package com.lunwen.lunwen.services.impl;

import com.lunwen.lunwen.bean.SysUserRole;
import com.lunwen.lunwen.bean.XueSheng;
import com.lunwen.lunwen.mapper.SysUserMapper;
import com.lunwen.lunwen.mapper.SysUserRoleMapper;
import com.lunwen.lunwen.mapper.XueShengMapper;
import com.lunwen.lunwen.services.SysUserRoleService;
import com.lunwen.lunwen.services.XueShengService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * (XueSheng)表服务实现类
 *
 * @author makejava
 * @since 2020-07-20 12:24:53
 */
@Service
public class XueShengServiceImpl implements XueShengService {

    @Autowired
    private XueShengMapper xueShengMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public XueSheng queryById(String id) {
        return this.xueShengMapper.queryById(id);
    }

    /**
     * 按用户ID查询
     * @param userId
     * @return
     */
    @Override
    public XueSheng queryByUserId(Long userId) {
        return this.xueShengMapper.queryByUserId(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<XueSheng> queryAllByLimit(XueSheng xueSheng, int offset, int limit) {
        return this.xueShengMapper.queryAllByLimit(xueSheng.getId(),
                                                    xueSheng.getBanjiId(),
                                                    xueSheng.getZhuanyeId(),
                                                    xueSheng.getXueyuanId(),
                                                    xueSheng.getUserId(),
                                                    offset,
                                                    limit);
    }

    @Override
    public XueSheng queryByUserName(String userName) {
        return this.xueShengMapper.queryByUserName(userName);
    }

    /**
     * 新增数据
     *
     * @param xueSheng 实例对象
     * @return 实例对象
     */
    @Override
    public XueSheng insert(XueSheng xueSheng) {
        this.sysUserMapper.insert(xueSheng.getSysUser());
        xueSheng.setUserId(this.sysUserMapper.queryByName(xueSheng.getSysUser().getUsername()).getUserId());
        this.xueShengMapper.insert(xueSheng);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(3L);
        sysUserRole.setUserId(xueSheng.getUserId());
        sysUserRoleMapper.insert(sysUserRole);
        return xueSheng;
    }

    /**
     * 修改数据
     *
     * @param xueSheng 实例对象
     * @return 实例对象
     */
    @Override
    public XueSheng update(XueSheng xueSheng) {
        xueSheng.getSysUser().setUserId(xueSheng.getUserId());
        this.sysUserMapper.update(xueSheng.getSysUser());
        this.xueShengMapper.update(xueSheng);
        return this.queryByUserId(xueSheng.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.xueShengMapper.deleteById(id) > 0;
    }

    @Override
    public int queryCount(XueSheng xueSheng) {
        return this.xueShengMapper.queryCount(xueSheng);
    }
}